/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.flipkart.ohos.proteus.slice;

import com.flipkart.ohos.proteus.*;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Image;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

import com.flipkart.ohos.proteus.api.ProteusManager;
import com.flipkart.ohos.proteus.exceptions.ProteusInflateException;
import com.flipkart.ohos.proteus.value.Layout;
import com.flipkart.ohos.proteus.value.ObjectValue;
import com.flipkart.ohos.proteus.value.Value;

import java.util.Map;

/**
 * 第二个显示页面
 *
 * @since 2021-05-10
 */
public class TwoAbilitySlice extends AbilitySlice implements ProteusManager.Listener {
    private ProteusLayoutInflater proteusLayoutInflater;
    private ObjectValue data;
    private Layout layout;
    private Styles styles;
    private Map<String, Layout> layouts;
    private ProteusManager proteusManager;

    private StackLayout container;

    private final StyleManager styleManager = new StyleManager() {
        @Override
        protected Styles getStyles() {
            return styles;
        }
    };

    private final LayoutManager layoutManager = new LayoutManager() {
        @Override
        protected Map<String, Layout> getLayouts() {
            return layouts;
        }
    };

    private final ProteusLayoutInflater.Callback callback = new ProteusLayoutInflater.Callback() {
        @Override
        public ProteusView onUnknownViewType(ProteusContext context, String type, Layout proteusLayout,
                                             ObjectValue objectValue, int index) {
            try {
                throw new ProteusInflateException("Unknown view type '" + type + "' cannot be inflated");
            } catch (ProteusInflateException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public void onEvent(String event, Value value, ProteusView view) {
            Button button = null;
            Text text = null;
            Image image = null;
            //此处用来处理点击事件等
            //获取控件类型
            String layoutType = view.getViewManager().getLayout().type;
            switch (layoutType) {
                case "Button":
                    button = (Button) view.getAsView();
                    LogUtil.debug("onEvent", "button=" + button.getText());
                    break;
                case "Text":
                    text = (Text) view.getAsView();
                    LogUtil.debug("onEvent", "text=" + text.getText());
                    if (value.isObject()) {
                        String jump = ((ObjectValue) value).getAsString("jump");
                        LogUtil.debug("onEvent", "jump=" + jump);
                        present(new WebViewAbilitySlice(), new Intent());
                    }
                    break;
                case "Image":
                    image = (Image) view.getAsView();
                    LogUtil.debug("onEvent", "image.width=" + image.getWidth());
                    break;
            }


        }
    };

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_two);
        container = (StackLayout) findComponentById(ResourceTable.Id_create_layout);
        proteusManager = DemoApplication.getProteusManager();
        ProteusContext proteusContext = proteusManager.getProteus().createContextBuilder(TwoAbilitySlice.this)
            .setLayoutManager(layoutManager)
            .setCallback(callback)
            .setStyleManager(styleManager)
            .build();
        proteusLayoutInflater = proteusContext.getInflater();
    }

    @Override
    public void onActive() {
        super.onActive();
        proteusManager.addListener(this);
        proteusManager.load();
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        proteusManager.removeListener(this);
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    void render() {
        // remove the current view
        container.removeAllComponents();
        // Inflate a new view using proteus
        ProteusView view = proteusLayoutInflater.inflate("TwoPageLayout", data);
        // Add the inflated view to the container
        container.addComponent(view.getAsView());
    }

    void reload() {
        proteusManager.update();
    }

    @Override
    public void onLoad() {
        data = proteusManager.getData();
        layouts = proteusManager.getLayouts();
        styles = proteusManager.getStyles();
        render();
    }

    @Override
    public void onError(String e) {
        new ToastDialog(this)
            .setText(e)
            .setAlignment(LayoutAlignment.CENTER)
            .show();
    }
}
